{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.8 \u56fa\u5b9a\u5927\u5c0f\u8bb0\u5f55\u7684\u6587\u4ef6\u8fed\u4ee3\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u8bb0\u5f55\u6216\u8005\u6570\u636e\u5757\u7684\u96c6\u5408\u4e0a\u8fed\u4ee3\uff0c\u800c\u4e0d\u662f\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u4e00\u884c\u4e00\u884c\u7684\u8fed\u4ee3\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u4e0b\u9762\u8fd9\u4e2a\u5c0f\u6280\u5de7\u4f7f\u7528 iter \u548c functools.partial() \u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import partial\n\nRECORD_SIZE = 32\n\nwith open('somefile.data', 'rb') as f:\n    records = iter(partial(f.read, RECORD_SIZE), b'')\n    for r in records:\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684 records \u5bf9\u8c61\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5b83\u4f1a\u4e0d\u65ad\u7684\u4ea7\u751f\u56fa\u5b9a\u5927\u5c0f\u7684\u6570\u636e\u5757\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u3002\n\u8981\u6ce8\u610f\u7684\u662f\u5982\u679c\u603b\u8bb0\u5f55\u5927\u5c0f\u4e0d\u662f\u5757\u5927\u5c0f\u7684\u6574\u6570\u500d\u7684\u8bdd\uff0c\u6700\u540e\u4e00\u4e2a\u8fd4\u56de\u5143\u7d20\u7684\u5b57\u8282\u6570\u4f1a\u6bd4\u671f\u671b\u503c\u5c11\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "iter() \u51fd\u6570\u6709\u4e00\u4e2a\u9c9c\u4e3a\u4eba\u77e5\u7684\u7279\u6027\u5c31\u662f\uff0c\u5982\u679c\u4f60\u7ed9\u5b83\u4f20\u9012\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\u548c\u4e00\u4e2a\u6807\u8bb0\u503c\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\n\u8fd9\u4e2a\u8fed\u4ee3\u5668\u4f1a\u4e00\u76f4\u8c03\u7528\u4f20\u5165\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u76f4\u5230\u5b83\u8fd4\u56de\u6807\u8bb0\u503c\u4e3a\u6b62\uff0c\u8fd9\u65f6\u5019\u8fed\u4ee3\u7ec8\u6b62\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4f8b\u5b50\u4e2d\uff0c functools.partial \u7528\u6765\u521b\u5efa\u4e00\u4e2a\u6bcf\u6b21\u88ab\u8c03\u7528\u65f6\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u56fa\u5b9a\u6570\u76ee\u5b57\u8282\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\n\u6807\u8bb0\u503c b'' \u5c31\u662f\u5f53\u5230\u8fbe\u6587\u4ef6\u7ed3\u5c3e\u65f6\u7684\u8fd4\u56de\u503c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u518d\u63d0\u4e00\u70b9\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u7684\u6587\u4ef6\u65f6\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u7684\u3002\n\u5982\u679c\u662f\u8bfb\u53d6\u56fa\u5b9a\u5927\u5c0f\u7684\u8bb0\u5f55\uff0c\u8fd9\u901a\u5e38\u662f\u6700\u666e\u904d\u7684\u60c5\u51b5\u3002\n\u800c\u5bf9\u4e8e\u6587\u672c\u6587\u4ef6\uff0c\u4e00\u884c\u4e00\u884c\u7684\u8bfb\u53d6(\u9ed8\u8ba4\u7684\u8fed\u4ee3\u884c\u4e3a)\u66f4\u666e\u904d\u70b9\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}